package com.example.smart_city_0829.activity.waimaidingcan;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.DeathData;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.waimai.OrderList;
import com.example.smart_city_0829.entity.waimai.WaiMaiTheme;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;

import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

public class OrderListActivity extends BaseActivity {

    private ListView lv;
    private List<OrderList.RowsBean> rows;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void initView() {
        lv = (ListView) findViewById(R.id.lv);

    }

    @Override
    protected void initData() {
        int position = getIntent().getIntExtra("position", 0);

        WaiMaiTheme.DataBean dataBean = DeathData.waiMaiTheme.getData().get(position);
        getSupportActionBar().setTitle("店家列表-" + dataBean.getThemeName());
        api.getOrderList(null, dataBean.getId()).enqueue(new NetCallBack<OrderList>() {
            @Override
            public void onResponse(Call<OrderList> call, Response<OrderList> response) {
                if (response.isSuccessful()) {
                    rows = response.body().getRows();
                    lv.setAdapter(new CommonAdapter<OrderList.RowsBean, CommonAdapter.CommListViewHolder>(
                            rows,
                            OrderListActivity.this
                    ) {
                        @Override
                        protected void setData(OrderList.RowsBean data, int position, CommListViewHolder viewHolder, View convertView, Context context) {
                            Glide.with(context)
                                    .load(Constant.BASE_URL2 + data.getImgUrl())
                                    .into(viewHolder.im);
                            viewHolder.title.setText(data.getName());
                            viewHolder.content.setText(data.getIntroduction());
                            viewHolder.tag2.setText("距离:" + data.getDistance());
                            viewHolder.tag3.setText("地址:" + data.getAddress());
                        }

                        @Override
                        protected CommListViewHolder getViewHolder() {
                            return new CommListViewHolder();
                        }

                        @Override
                        protected void initView(CommListViewHolder viewHolder, View convertView) {
                            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
                            viewHolder.im = (ImageView) convertView.findViewById(R.id.im);
                            viewHolder.content = (TextView) convertView.findViewById(R.id.content);
                            viewHolder.tag1 = (TextView) convertView.findViewById(R.id.tag1);
                            viewHolder.tag2 = (TextView) convertView.findViewById(R.id.tag2);
                            viewHolder.tag3 = (TextView) convertView.findViewById(R.id.tag3);
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                OrderList.RowsBean rowsBean = rows.get(position);
                Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
//                intent.putExtra("data",gson.toJson(rowsBean));
                intent.putExtra("id",rowsBean.getId());
                startActivity(intent);
            }
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "店家列表";
    }
}